package com.biz.impl;

import com.biz.CustomerConnectBiz;
import com.core.BizException;
import com.dao.CustomerDao;
import com.entity.model.Customer;
import com.biz.CustomerBiz;
import com.core.AbstractBiz;
import com.entity.model.CustomerConnect;
import com.entity.threeConnect.ThirdUserInfo;
import com.util.webpage.PageData;
import org.apache.commons.lang3.CharUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * Created by jc on 2019/11/16.
 */
@Service
@Transactional
public class CustomerBizImpl extends AbstractBiz<Customer> implements CustomerBiz {
    @Resource
    private CustomerDao customerDao;

    @Resource
    private CustomerConnectBiz customerConnectBiz;


    @Override
    public Customer getByConnect(Map cond) throws BizException {
        return customerDao.getByConnect(cond);
    }

    @Override
    public Customer addCustomerByConnect(ThirdUserInfo thirdUserInfo) {
        Customer customer = new Customer();
        customer.setAccount(thirdUserInfo.getPhone());
        customer.setSex(thirdUserInfo.getSex().toString());
        customer.setHeadImgUrl(thirdUserInfo.getHeadImgUrl());
        customer.setCityCode(thirdUserInfo.getCity());
        customer.setProvinceCode(thirdUserInfo.getProvince());
        customer.setNickName(thirdUserInfo.getNickname());
        this.save(customer);
        CustomerConnect customerConnect = new CustomerConnect();
        customerConnect.setCustomerId(customer.getId());
        customerConnect.setUnionid(thirdUserInfo.getUnionid());
        customerConnect.setOpenid(thirdUserInfo.getOpenId());
        customerConnectBiz.save(customerConnect);
        return customer;
    }


    @Override
    public void updateCustomerByPhoneAndConnect(Integer customerId, String phone) {
        Customer user = new Customer();
        user.setId(customerId);
        user.setAccount(phone);
        this.update(user);
    }

    @Override
    public Customer getByAccount(String account) throws BizException {
        if (StringUtils.isBlank(account)) {
            throw new BizException("手机号不能为空");
        }
        Condition cond = new Condition(Customer.class);
        cond.createCriteria().andEqualTo("account", account);
        List<Customer> list = findByCondition(cond);
        return list == null || list.size() == 0 ? null : list.get(0);
    }

    @Override
    public List<Customer> customerList(PageData cond) {
        return customerDao.customerList(cond);
    }

    @Override
    public List<Customer> pdCustomerList(PageData cond) {
        return customerDao.pdCustomerList(cond);
    }
}
